package com.register.client;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.register.client.model.RegisterInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RegisterUtil {

    @Value("${register.center.host:null}")
    private String host;
    @Value("${register.center.port:null}")
    private String port;


    public void registerApi(String apiName, String host, int port){
        String url = StrUtil.format("http://{}:{}/register", this.host, this.port);
        RegisterInfo registerInfo = new RegisterInfo();
        registerInfo.setApiName(apiName);
        registerInfo.setHost(host);
        registerInfo.setPort(port);

        String params = JSONUtil.toJsonStr(registerInfo);
        String post = HttpUtil.post(url, params);
        Console.log("register to center result : {}", post);
    }
}
